package Components
{
	import flash.geom.Point;

	public class MovementComponent
	{
		public var direction:Point = new Point(0,0);
		public var location:Point = new Point(0,0);
		public var destination:Point = new Point(0,0);
		public var speed:Number = 4;
		
		public function MovementComponent()
		{
		}
		
		public function isInRange(range:Number):Boolean{
			return destination.subtract(location).length <= range;
		}
		
		public function isAtDestination():Boolean{
			return isInRange(0);
		}
		
		public function stop():void{
			destination = location.clone();			
		}
		
		public function update(delta:int):void{
			var destinationVector:Point = destination.subtract(location);
			var destinationDistance:Number = destinationVector.length;

			if(destinationDistance > 0){
				var projectedDistance:Number = direction.length * speed * delta/1000;
				var nextDirection:Point = destinationVector.clone(); 
				nextDirection.normalize(1);
				
				if(projectedDistance > destinationDistance){
					location = destination.clone();
				} else {
					var travelVector:Point = nextDirection.clone();
					travelVector.normalize(speed*delta/1000);
					location = location.add(travelVector);
				}
				
				direction = nextDirection;
			}			
		}
	}
}